Android 最简单实用的打开手机通知权限设置 您所在的位置:网站首页 小米11PrOUS|M卡应用允许通知开关怎样打开 Android 最简单实用的打开手机通知权限设置

Android 最简单实用的打开手机通知权限设置

2024-06-28 04:47| 来源: 网络整理| 查看: 265

最近做离线的消息推送,发现oppo、vivo、小米等有些手机机型通知权限默认是关闭的,需要手动打开,网上有很多介绍怎么设置和打开的,但是发现都要适配各种版本和各大机型,太麻烦和繁琐,不是我想要的效果,于是自己整理了一个简单方便实用的方法,不多说,直接上代码:

1.封装的方法如下: /** * 打开通知权限 * * @param context */ public static void openNotificationSettingsForApp(Context context) { // Links to this app's notification settings. Intent intent = new Intent(); intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); intent.putExtra("app_package", context.getPackageName()); intent.putExtra("app_uid", context.getApplicationInfo().uid); // for Android 8 and above intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName()); context.startActivity(intent); }

2.在MainActivity使用:

NotificationManagerCompat notification = NotificationManagerCompat.from(mContext); boolean isEnabled = notification.areNotificationsEnabled(); if (!isEnabled) { toTipDialog(); }

3.完整的工具类代码如下: /** * @auth: njb * @date: 2021/7/28 10:54 * @desc: 手机通知设置工具类 */ public class NotifyManagerUtils { static long[] vibrate = {100, 200, 300, 400, 500, 400, 300, 200, 400}; /** * 打开通知权限 * * @param context */ public static void openNotificationSettingsForApp(Context context) { // Links to this app's notification settings. Intent intent = new Intent(); intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); intent.putExtra("app_package", context.getPackageName()); intent.putExtra("app_uid", context.getApplicationInfo().uid); // for Android 8 and above intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName()); context.startActivity(intent); } public static void notificationActions(Context context,String content,String title) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); RemoteViews remoteViews = new RemoteViews(CommentUtils.getPackageName(context), R.layout.layout_notify); remoteViews.setTextViewText(R.id.tv_title, title); Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.mipmap.app_icon_round_connor); builder.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)); builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.app_icon_round_connor)); builder.setContentTitle(title); builder.setContentText(content); builder.setContent(remoteViews); builder.setWhen(System.currentTimeMillis()); builder.setAutoCancel(true); builder.setContentIntent(pendingIntent); // 用户可以看到的通知渠道的名字. CharSequence name = "notification channel"; // 用户可以看到的通知渠道的描述 NotificationChannel mChannel = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { mChannel = new NotificationChannel("1", name, NotificationManager.IMPORTANCE_HIGH); // 配置通知渠道的属性 mChannel.setDescription(content); // 设置通知出现时的闪灯(如果 android 设备支持的话) mChannel.enableLights(true); mChannel.setLightColor(Color.RED); // 设置通知出现时的震动(如果 android 设备支持的话) mChannel.enableVibration(true); mChannel.setVibrationPattern(vibrate); //最后在notificationmanager中创建该通知渠道 notificationManager.createNotificationChannel(mChannel); } notificationManager.notify(1, builder.build()); } }

4.是不是发现很简单有没有,一个方法就搞定,而且是适配了各大厂商机型,目前亲自测试了小米、华为、oppo、Vivo、三星、google、一加等多款机型都可以正常跳转到本应用的通知设置界面,没有那么多版本判断和机型适配代码,去掉繁琐的使用,如果大家觉得有用可以自己去试试,亲自体验一下,毕竟说再多再好没有实战数据说明来得真实。因为源码中有判断版本,明天会放出运行效果,好了,今天先到这里,本菜鸡准备休息了~~

关键系统源码如下:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有